<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link https://swoft.org
 * @document https://swoft.org/docs
 * @contact group@swoft.org
 * @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Http\Controller\Admin;

use App\Exception\HttpException;
use App\Model\Logic\UploadLogic;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Http\Message\ContentType;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\Middleware;
use Swoft\Http\Server\Annotation\Mapping\Middlewares;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use App\Http\Middleware\CheckLoginMiddleware;
use Swoft\Http\Message\Response;

/**
 * Class IndexController
 *
 * @Controller(prefix="/admin")
 * @Middlewares({
 *     @Middleware(CheckLoginMiddleware::class)
 *     })
 * @package App\Http\Controller\Admin
 */
class IndexController{
    /**
     * @RequestMapping(route="/admin", method=RequestMethod::GET)
     * @return \Swoft\Http\Message\Response
     * @throws \Throwable
     */
    public function index()
    {
        return view('admin/index/index');
    }

    /**
     * 后台菜单接口
     * @RequestMapping(route="menu", method=RequestMethod::GET)
     * @return \Swoft\Http\Message\Response
     * @throws \Throwable
     */
    public function menu(Response $response)
    {
        $menu = config('menu');
        return $response->withContentType(ContentType::JSON)->withStatus(200)->withData($menu);
    }

    /**
     * @RequestMapping(route="welcome", method=RequestMethod::GET)
     * @return \Swoft\Http\Message\Response
     * @throws \Throwable
     */
    public function welcome()
    {
        return view('admin/index/welcome');
    }

    /**
     * @Inject()
     * @var UploadLogic
     */
    private $uploadLogic;

    /**
     * @RequestMapping(route="upload", method=RequestMethod::POST)
     * @return \Swoft\Http\Message\Response
     * @throws \Throwable
     */
    public function upload(Request $request)
    {
        $file = $request->getUploadedFiles();
        if($file){
            $this->uploadLogic->upload($file['file']);
        }else{
            throw new HttpException('请选择上传文件');
        }
    }
}
